package cn.com.khproc;

import com.elite.groovy.util.EliteBean;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.util.List;
import java.util.Map;

/**
 * @Title: callProcedure
 * @Description: 执行存储过程调用由jsonCallProcedure完成校验Json后执行
 * @author: LiJuntao
 * @date 2019-12-18 下午9:16:40
 */
public class callProcedure {
    private static final Log log = LogFactory.getLog(callProcedure.class);

    public static String doService(String dataSource, String SyncTask, List<Map<String, Object>> paramList, String SyncJson) {
        EliteBean eliteBean = new EliteBean();
        JSONObject reJson = new JSONObject();

        String reString = null;

        try {
            JSONObject json = JSONObject.fromObject(SyncJson);

            // 拼接调用存储过程的语句
            String execSQL = "{call " + ((Map<?, ?>) paramList.get(0)).get("Sync_Object").toString() + "(?";
            for (int i = 1; i < paramList.size(); i++) {
                execSQL = execSQL + ",?";
            }
            execSQL = execSQL + ")}";
            log.debug("执行调用存储过程SQL:" + execSQL);

            String in_Rank = null;
            String in_Name = null;
            String in_Type = null;
            String in_Mode = null;
            String in_Value = null;

            // 调用前参数信息处理
            JSONObject inObj = new JSONObject();
            JSONArray inArr = new JSONArray();

            for (int i = 0; i < paramList.size(); i++) {
                in_Rank = ((Map<?, ?>) paramList.get(i)).get("Param_Rank").toString();
                in_Name = ((Map<?, ?>) paramList.get(i)).get("Param_Name").toString().toLowerCase();

                in_Type = ((Map<?, ?>) paramList.get(i)).get("Param_Type").toString().toLowerCase();
                in_Mode = ((Map<?, ?>) paramList.get(i)).get("Param_Mode").toString().toLowerCase();
                in_Value = GetJV(json, in_Name);

                log.debug("处理参数信息 Rank:" + in_Rank + " Name:" + in_Name + " Type:" + in_Type + " Mode:" + in_Mode + " Value:" + in_Value);

                inObj.put("type", in_Type);
                inObj.put("mode", in_Mode);
                inObj.put("value", in_Value);

                inArr.add(inObj.toString());

            }

            log.debug("执行调用存储过程参数Array:" + inArr.toString());
            List<?> execList = eliteBean.executeWithMultiReturn(execSQL, dataSource, inArr.toString());

            // 循环获取出参结果
            int outPNum = 0;
            for (int j = 0; j < paramList.size(); j++) {
                if (((Map<?, ?>) paramList.get(j)).get("Param_Mode").toString().equals("out")) {

                    String out_Rank = ((Map<?, ?>) paramList.get(j)).get("Param_Rank").toString();
                    String out_Name = ((Map<?, ?>) paramList.get(j)).get("Param_Name").toString().toLowerCase();
                    String out_Type = ((Map<?, ?>) paramList.get(j)).get("Param_Type").toString().toLowerCase();

                    String out_Result = null;

                    try {
                        out_Result = (String) execList.get(outPNum);

                    } catch (Exception e) {
                        out_Result = "";
                    }

                    log.debug("返回参数信息(" + (outPNum + 1) + ") 出参位置:" + out_Rank + " Name:" + out_Name + " out_Type:" + out_Type + " 出参名称:" + out_Name + " 出参结果:" + out_Result);

                    reJson.put(out_Name, out_Result);

                    outPNum++;

                }

            }

            reString = reJson.toString();

            log.debug("执行调用存储过程Object:" + reString);

        } catch (Exception e) {
            log.error("执行调用存储过程 Exception:" + e.getMessage());
            reString = "";
        }

        log.debug("执行调用存储过程返回结果集合:" + reString);
        return reString;
    }

    // 解析Json值
    public static String GetJV(JSONObject inJson, String getKey) {
        String Rev_Value = null;
        try {
            Rev_Value = inJson.getString(getKey);
        } catch (Exception e) {
            try {
                Rev_Value = String.valueOf(inJson.getInt(getKey));
            } catch (Exception e1) {
                try {
                    Rev_Value = String.valueOf(inJson.getJSONArray(getKey));
                } catch (Exception e2) {
                    try {
                        Rev_Value = String.valueOf(inJson.getJSONObject(getKey));
                    } catch (Exception e3) {

                        try {
                            Rev_Value = String.valueOf(inJson.getBoolean(getKey));
                        } catch (Exception e4) {
                            Rev_Value = "";
                        }
                    }
                }
            }
        } finally {
            if (Rev_Value == null || Rev_Value.equals("null") || Rev_Value.length() <= 0) {
                Rev_Value = "";
            } else {
            }
        }
        return Rev_Value;
    }

}